package com.ctz.commonframe.utils;

import java.io.File;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;

public class FilePathUtil {
	public static final String AUDIO_FOLDER = "/Audio/";
	public static final String VIDEO_FOLDER = "/Video/";
	public static final String IMAGE_FOLDER = "/Image/";
	public static final String CACHE_FOLDER = "/Cache/";
	public static final String ATTACHMENT_FOLDER = "/Attachment/";
	public static final String LOG_FOLDER = "/Log/";
	public static final String DOWNLOAD_FOLD = "/Download/";
	
	public static final String PREFIX_IMAGE = "IMAGE_";
	public static final String PREFIX_AUDIO = "AUDIO_";
	public static final String PREFIX_VIDEO = "VIDEO_";
	public static final String PREFIX_CACHE = "CACHE_";
	public static final String PREFIX_LOG = "LOG_";
	
	public static final String SUFFIX_IMAGE = ".jpg";
	public static final String SUFFIX_AUDIO = ".mp3";
	public static final String SUFFIX_TXT = ".txt";
	
	public static String getAudioFilePath(Context context){
		String rootPath = getFileRootPath(context);
		if(rootPath != null){
			return rootPath + AUDIO_FOLDER;
		}
		return null;
	}
	
	public static String getVideoFilePath(Context context){
		String rootPath = getFileRootPath(context);
		if(rootPath != null){
			return rootPath + VIDEO_FOLDER;
		}
		return null;
	}
	
	public static String getImageFilePath(Context context){
		String rootPath = getFileRootPath(context);
		if(rootPath != null){
			return rootPath + IMAGE_FOLDER;
		}
		return null;
	}
	
	public static String getCacheFolderPath(Context context, String folderName){
		String rootPath = getCacheRootPath(context);
		if(!TextUtils.isEmpty(folderName)){
			String path = rootPath + File.separator;
			if(createIfNotExist(path)){
				return path;
			}
		}
		return rootPath;
	}
	
	public static String getLogFilePath(Context context){
		String rootPath = getFileRootPath(context);
		if(rootPath != null){
			return rootPath + LOG_FOLDER;
		}
		return null;
	}
	
	public static String getAttachmentFilePath(Context context){
		String rootPath = getFileRootPath(context);
		if(rootPath != null){
			return rootPath + ATTACHMENT_FOLDER;
		}
		return null;
	}
	
	public static boolean createIfNotExist(String path){
		if(TextUtils.isEmpty(path)){
			return false;
		}
		File file = new File(path);
		if(!file.exists()){
			if(!file.mkdirs()){
				return false;
			}
		}
		return true;
	}
	
	public static String getDownloadPath(Context context){
		String rootPath = getFileRootPath(context);
		if(rootPath != null){
			return rootPath + DOWNLOAD_FOLD;
		}
		return null;
	}
	
	public static String getImageFileName(){
		return PREFIX_IMAGE + System.currentTimeMillis() + SUFFIX_IMAGE;
	}
	
	public static String getAudioFileName(){
		return PREFIX_AUDIO + System.currentTimeMillis() + SUFFIX_AUDIO;
	}
	
	public static String getVideoFileName(String suffix){
		return PREFIX_VIDEO + System.currentTimeMillis() + suffix;
	}

	public static String getRootPath(){
		if(isSdCardEnabled()){
			return Environment.getExternalStorageDirectory().getAbsolutePath();
		}
		return null;
	}

	/**
	 * 获取文件的存储根目录，用于应用被卸载后也不删除的文件
	 * @param context
	 * @return
	 */
	public static String getFileRootPath(Context context){
		String filePath;
		if(isSdCardEnabled()){
			filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + context.getPackageName();
		}else{
			filePath = context.getFilesDir().getPath();
		}
		return filePath;
	}

	/**
	 * 获取缓存的存储根目录，随着应用被卸载会同步被系统删除
	 * @param context
	 * @return
	 */
	public static String getCacheRootPath(Context context) {
		String cachePath;
		if (isSdCardEnabled()) {
			cachePath = context.getExternalCacheDir().getPath();
		} else {
			cachePath = context.getCacheDir().getPath();
		}
		return cachePath;
	}

	public static boolean isSdCardEnabled() {
	    return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
				|| !Environment.isExternalStorageRemovable();
	}
}
